//
//  NewOrderModel.swift
//  Employee
//
//  Created by swordjoy on 2017/4/7.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

public struct NewOrderModel {
  
  var imageModels: [ImageIdModel] = [] {
    
    didSet {
      
      let tempArray = imageModels.map ({
        return $0.fileId
      })
      
      imageIds = tempArray.joined(separator: ",")
      
    }
    
  }
  
  var title = ""
  var content = ""
  var categoryId = ""
  var priorityLevel = ""
  var accept = ""
  var acceptStaffId = ""
  var acceptOrgId = ""
  var imageIds = ""
  
}

struct Priority {
  
  let title: String
  let tag: String
  
  init(priority: (String, String)) {
    self.tag = priority.0
    self.title = priority.1
  }
  
  static func array(from titles: [(String, String)]) -> [Priority] {
    return titles.map({ return Priority(priority: $0) })
  }
  
}

struct Department: ModelProtocol {
  
  let name: String
  let id: String
  let staffs: [Staff]
  
  init(json: JSON) {
    self.name = json["orgName"].stringValue
    self.id = json["orgId"].stringValue
    
    if json["staffs"].arrayValue.isEmpty {
      let staff = Staff(staffName: "所有人", staffId: "")
      staffs = [staff]
    }
    else {
      staffs = Staff.array(from: json["staffs"].arrayValue)
    }
  }
  
  init(name: String, id: String, staffs: [Staff]) {
    self.name = name
    self.id = id
    self.staffs = []
  }
  
  static func array(from jsonArray: [JSON]) -> [Department] {
    return jsonArray.map({ return Department(json: $0) })
  }
  
}


struct Staff: ModelProtocol {
  
  let staffName: String
  let staffId: String
  
  init(json: JSON) {
    self.staffName = json["staffName"].stringValue
    self.staffId = json["staffId"].stringValue
  }
  
  init(staffName: String, staffId: String) {
    self.staffName = staffName
    self.staffId = staffId
  }
  
  static func array(from jsonArray: [JSON]) -> [Staff] {
    
    var temp = jsonArray.map({ return Staff(json: $0) })
    
    if temp.first?.staffName == "电表" {
        return temp
    }

    let staff = Staff(staffName: "所有人", staffId: "")
    temp.insert(staff, at: 0)
    return temp
  }
  
}
